package cn.sinata.wanglin.mine.fragment;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.fragment.RecyclerViewFragment;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;
import java.util.ArrayList;
import java.util.List;
import cn.sinata.wanglin.adapter.MessageAdapter;
import cn.sinata.wanglin.adapter.NoticeAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.mine.activity.MessageDetailActivity;
import cn.sinata.wanglin.model.Message;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
/**
 *我的--平台公告，系统消息
 *
 */
public class MessagesFragment extends RecyclerViewFragment {

    private BaseRecyclerAdapter messageAdapter;
    private List<Message> messages = new ArrayList<>();

    private int type;
    private int pageNo = 0;
    private String userId;

    public static MessagesFragment newInstance(int type){
        MessagesFragment messagesFragment = new MessagesFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type",type);
        messagesFragment.setArguments(bundle);
        return messagesFragment;
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        if (getArguments()!=null){
            type = getArguments().getInt("type");
        }
        Utils.systemErr("---->"+type);
        if (type == 1){
            messageAdapter = new MessageAdapter(messages);

        }else {
            messageAdapter = new NoticeAdapter(messages);
            messageAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Message message = messages.get(position);

                    ActivityUtil.create(MessagesFragment.this).go(MessageDetailActivity.class)
                            .put("message",message)
                            .start();
                }
            });
        }
        return messageAdapter;
    }

    @Override
    protected void onFirstVisibleToUser() {
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
        setItemDecoration(DEFAULT_MARGIN_DIVIDER);
        showDialog();
        getData();
    }

    private void getData(){
        HttpManager.getNoticeOrMessages(userId,type,pageNo).subscribe(new ResultDataSubscriber<ArrayList<Message>>(this) {
            @Override
            public void onSuccess(String msg, ArrayList<Message> datas) {
                if (pageNo == 0) {
                    messages.clear();
                }
                if (datas.size() == 0) {
                    if (pageNo == 0) {
                        setLoadMoreText("暂无数据");
                    } else {
                        pageNo --;
                        setLoadMoreText("没有更多了");
                    }
                }
                messages.addAll(datas);
                messageAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void onVisibleToUser() {

    }

    @Override
    protected void onInvisibleToUser() {

    }
    @Override
    protected void pullDownRefresh() {
        pageNo = 1;
        getData();
    }

    @Override
    protected void loadMore() {
        pageNo++;
        getData();
    }
}
